import time
from xtquant.xttrader import XtQuantTrader
from xtquant.xttype import StockAccount
from QMT  import MyXtQuantTraderCallback
from 基础配置 import settings
import asyncio

def save_orders():
    # 初始化交易接口
    path = settings.QMT_DIR
    session_id = int(time.time())
    xt_trader = XtQuantTrader(path, session_id)
    acc = StockAccount(settings.ACC, 'STOCK')
    qmt = MyXtQuantTraderCallback(acc,xt_trader)
    xt_trader.register_callback(qmt)
    xt_trader.start()

    # 连接交易账号
    connect_result = xt_trader.connect()
    if connect_result == 0:
        print('交易连接成功')  # 0=成功
        subscribe_result = xt_trader.subscribe(acc)
        if subscribe_result == 0:
            print("订阅交易回报成功")  # 0=成功
        else:
            print("订阅交易回报失败")  # 0=成功
    else:
        print("交易连接失败，请检查账号或路径！")
        exit()

    qmt.save_orders()

async def main():
    save_orders()

if __name__ == "__main__":
    asyncio.run(main())